package bma.common.netty.webserver;

import bma.common.langutil.ai.stack.AIStack;

public abstract class RequestDispatcher implements RequestProcessor {

	public abstract boolean dispatch(AIStack<Boolean> stack, RequestContext ctx)
			throws Exception;

	public boolean process(AIStack<Boolean> stack, RequestContext ctx)
			throws Exception {
		if (!this.dispatch(stack, ctx)) {
			WebServerUtil.endWithNotFound("CORE", ctx);
		}
		return true;
	};

	public static boolean dispatchOrProcess(AIStack<Boolean> stack,
			RequestProcessor p, RequestContext ctx) throws Exception {
		if (p == null) {
			return false;
		}
		if (p instanceof RequestDispatcher) {
			RequestDispatcher dis = (RequestDispatcher) p;
			return dis.dispatch(stack, ctx);
		}
		p.process(stack, ctx);
		return true;
	}
}
